package com.acmen.easyapi.repository;

import com.acmen.easyapi.dbcp.JdbcService;
import com.acmen.easyapi.dto.UnitTestAdd;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * @author acmen
 */
@Component
public class UnitTestRepository {
    private static final String QUERY_COMB_TESTS_BY_API_SQL = "select id,name,headers,params,body,api_id apiId,result from t_unit_test where join_comb_test=1 and api_id =?";
    private static final String QUERY_BY_APP_SQL = "select t.*,t.join_comb_test as joinPressureTest from t_unit_test t where app_name=?";
    private final static String QUERY_BY_API_SQL = "select t.*,t.join_comb_test as joinPressureTest from t_unit_test t where api_id=? order by create_dt";
    private final static String INSERT_SQL = "insert into t_unit_test(id,app_name,api_id,name,path,method,params,body,result,create_dt,headers) values(?,?,?,?,?,?,?,?,?,?,?)";
    private final static String DELETE_SQL = "delete from t_unit_test where id=?";
    private final static String UPDATE_SQL = "update t_unit_test set join_comb_test=? where id=?";
    private final static String QUERY_SET_BY_APP_SQL  = "select DISTINCT api_id apiId from t_unit_test where join_comb_test=1 and app_name=?";

    private static final String QUERY_BY_CASE_SQL = "select t.* from t_unit_test t where id in(select unit_test_id from test_case_unit where test_case_id=?)";

    @Resource
    private JdbcService jdbcService;


    /**
     * 根据接口信息ID查询出单元测试记录
     *
     * @param apiId
     * @return
     */
    public List<UnitTestAdd> selectList(String apiId) {
        List<UnitTestAdd> unitTests = jdbcService.selectList(QUERY_BY_API_SQL, new Object[]{apiId}, UnitTestAdd.class);
        return unitTests;
    }


    public List<UnitTestAdd> selectCombTestList(String apiId) {
        return jdbcService.selectList(QUERY_COMB_TESTS_BY_API_SQL, new Object[]{apiId}, UnitTestAdd.class);
    }

    public List<UnitTestAdd> selectListByCase(String testCaseId) {
        List<UnitTestAdd> unitTests = jdbcService.selectList(QUERY_BY_CASE_SQL, new Object[]{testCaseId}, UnitTestAdd.class);
        return unitTests;
    }

    public List<UnitTestAdd> selectListByAppName(String apiName) {
        List<UnitTestAdd> unitTests = jdbcService.selectList(QUERY_BY_APP_SQL, new Object[]{apiName}, UnitTestAdd.class);
        return unitTests;
    }

    public Set<String> queryForSetByApp(String name) {
        List<Map> dataList = jdbcService.selectList(QUERY_SET_BY_APP_SQL, new Object[]{name});
        Set<String> set = new HashSet<>();
        for(Map rowMap : dataList){
            set.add((String)rowMap.get("apiId"));
        }
        return set;
    }


    /**
     * 保存单元测试
     * id,api_id,name,path,method,param,result,create_dt,headers
     *
     * @param unitTest
     * @return
     */
    public boolean save(UnitTestAdd unitTest) {
        Object[] args = {unitTest.getId(),unitTest.getAppName(), unitTest.getApiId(), unitTest.getName(),
                unitTest.getPath(), unitTest.getMethod(), unitTest.getParams(),unitTest.getBody(), unitTest.getResult(), unitTest.getCreateDt(),
                unitTest.getHeaders()};
        return jdbcService.update(INSERT_SQL, args) > 0;
    }

    /**
     * 删除单元测试
     *
     * @param id
     * @return
     */
    public boolean delete(String id) {
        return jdbcService.update(DELETE_SQL, new Object[]{id}) > 0;
    }

    public boolean joinPressureTest(String id,boolean join) {
        return jdbcService.update(UPDATE_SQL, new Object[]{join?1:0,id}) > 0;
    }


}
